//
//  JRNewFeatureCollectionViewController.swift
//  myWeibo
//
//  Created by 樊健荣 on 16/7/21.
//  Copyright © 2016年 樊健荣. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let ItemCount = 4
class JRNewFeatureCollectionViewController: UICollectionViewController {

    
    let layout = UICollectionViewFlowLayout()
    // MARK: - 生命周期
    init() {
//        UICollectionViewFlowLayout()
        
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView?.backgroundColor = UIColor.brownColor()
        
        self.collectionView!.registerClass(JRNewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //设置cell
        setupLayout()
        
    }
    
    
    // MARK: - layout
    private func setupLayout() {
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }
    


}


// MARK: - 数据源方法
extension JRNewFeatureCollectionViewController {
    /// 返回cell数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    
    /// 返回cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 根据Identifier 去缓存池中找 cell,如果找不到dequeue这个方法会根据注册的cell去创建一个新的cell返回给我们
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JRNewFeatureCollectionViewCell
        
//        cell.backgroundColor = UIColor.randomColor()
        
        cell.image = UIImage(named: "new_feature_\(indexPath.item + 1)")
        
        // 只有最后一页才需要按钮
        if indexPath.item == ItemCount - 1 {
            // 最后一页
            cell.showButtons(true)
        } else {
            cell.showButtons(false)
        }
        
        return cell
    }
}
